/**
 * 
 */
package spatial.join;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import indexing.grid.CellID;

import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.OutputCollector;
import org.junit.Test;

import spatial.join.RasterApp.RasterAppMapper;
import spatial.join.RasterApp.RasterAppReducer;

/**
 * @author Xiaohui int[] xs2 = {0, 4, 0, 4}; int[] ys2 = {4, 4, 0, 0};
 */
public class RasterAppTest {
	final static String sep = ",";
	final static String plgsep = "-";

	@Test
	public void processPolygon() throws IOException {
		RasterAppMapper mapper = new RasterAppMapper();
		Text poly1 = new Text("1 4 0 0 5 5 0 5 5 0");
		OutputCollector<Text, Text> output = mock(OutputCollector.class);
		mapper.map(null, poly1, output, null);

		verify(output).collect(new Text(new CellID(0, 0).toString()),
				new Text("full" + sep + poly1.toString()));
		verify(output).collect(new Text(new CellID(1, 0).toString()),
				new Text("weak" + sep + poly1.toString()));
		verify(output).collect(new Text(new CellID(0, 1).toString()),
				new Text("weak" + sep + poly1.toString()));
		verify(output).collect(new Text(new CellID(1, 1).toString()),
				new Text("weak" + sep + poly1.toString()));
	}
	@Test
	public void testReducer() throws IOException{
		//Expect key = cellid
		Text poly1 = new Text("1 4 0 0 5 5 0 5 5 0");
		Text poly2 = new Text("2 4 2 2 7 7 1 3 3 1");
		Text key = new Text( new CellID(0,0).toString());
		Iterator<Text> values = Arrays.asList(
			    new Text("full"+sep+poly1.toString()), new Text("weak"+sep+poly2.toString())).iterator();

		RasterAppReducer reducer = new RasterAppReducer();
		OutputCollector<Text, Text> output = mock(OutputCollector.class);
		reducer.reduce(key, values, output, null);

		verify(output).collect(new Text(""), new Text("Overlap: "+poly1.toString()+plgsep+poly2.toString()));
	}
}
